package cn.fl.custpay.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cmm.mid.core.framework.page.ListPageVO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.fl.custpay.condition.CustPayCfgDQC;
import cn.fl.custpay.service.ICustPayCfgDService;
import cn.fl.custpay.vo.CustPayCfgDVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import cn.fl.framework.base.context.CurrentThreadContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.fl.system.constant.SysLogConstant;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;

/**
 * @Description: 支付渠道配置子表 控制层
 */
@Controller
@RequestMapping("/custpay/custPayCfgD")
public class CustPayCfgDController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CustPayCfgDController.class);

	/**
	 * CustPayCfgD 服务层BEAN
	 */
	@Autowired
	private ICustPayCfgDService custPayCfgDService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("riskctrl/custPayCfgD/list");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("riskctrl/custPayCfgD/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		CustPayCfgDQC qc = new CustPayCfgDQC();
		if (StringUtils.isNotEmpty(searchName)) {

		}
		page.setCondition(qc);

		Pager pager = this.custPayCfgDService.searchListPage(page);
		List<CustPayCfgDVO> list = (List<CustPayCfgDVO>) pager.getResultList();
		ListPageVO<CustPayCfgDVO> listPage = new ListPageVO<CustPayCfgDVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.custPayCfgDService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			CustPayCfgDVO vo = this.custPayCfgDService.selectById(Long.valueOf(id));
			if (StringUtils.isNotEmpty(vo.getOnceLimit())) {
				vo.setOnceLimitNm(DecimalUtils.convertFenToYuan(vo.getOnceLimit()));
			}
			if (StringUtils.isNotEmpty(vo.getDayLimit())) {
				vo.setDayLimitNm(DecimalUtils.convertFenToYuan(vo.getDayLimit()));
			}
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 得到一条记录
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getByRelationId.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getByRelationId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String relationId = request.getParameter("relationId");
		CustPayCfgDQC qc = new CustPayCfgDQC();
		if (StringUtils.isNotEmpty(relationId)) {
			qc.setRelationId(Long.valueOf(relationId));
		}
		List<CustPayCfgDVO> list = this.custPayCfgDService.searchList(qc);
		ListPageVO<CustPayCfgDVO> listPage = new ListPageVO<CustPayCfgDVO>(list, list.size());
		return listPage;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String relationId = request.getParameter("relationId");
		String bkCd = request.getParameter("bkCd");
		String sortNo = request.getParameter("sortNo");
		String onceLimitNm = request.getParameter("onceLimitNm");
		String dayLimitNm = request.getParameter("dayLimitNm");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		CustPayCfgDVO entity = null;
		try {
			entity = new CustPayCfgDVO();
			if (StringUtils.isNotEmpty(relationId)) {
				entity.setRelationId(Long.valueOf(relationId));
			}
			if (StringUtils.isNotEmpty(bkCd)) {
				entity.setBkCd(bkCd);
			}
			if (StringUtils.isNotEmpty(sortNo)) {
				entity.setSortNo(Long.valueOf(sortNo));
			}
			if (StringUtils.isNotEmpty(onceLimitNm)) {
				entity.setOnceLimit(DecimalUtils.convertYuanToFen(Double.valueOf(onceLimitNm)));
			}
			if (StringUtils.isNotEmpty(dayLimitNm)) {
				entity.setDayLimit(DecimalUtils.convertYuanToFen(Double.valueOf(dayLimitNm)));
			}
			entity.setIsValid(BaseConstant.IS_YESNO_YES);
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			Long id = this.custPayCfgDService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String relationId = request.getParameter("relationId");
		String bkCd = request.getParameter("bkCd");
		String sortNo = request.getParameter("sortNo");
		String onceLimitNm = request.getParameter("onceLimitNm");
		String dayLimitNm = request.getParameter("dayLimitNm");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CustPayCfgDVO entity = null;
		try {
			entity = new CustPayCfgDVO();
			entity.setId(Long.valueOf(id));
			if (StringUtils.isNotEmpty(relationId)) {
				entity.setRelationId(Long.valueOf(relationId));
			}
			if (StringUtils.isNotEmpty(bkCd)) {
				entity.setBkCd(bkCd);
			}
			if (StringUtils.isNotEmpty(sortNo)) {
				entity.setSortNo(Long.valueOf(sortNo));
			}
			if (StringUtils.isNotEmpty(onceLimitNm)) {
				entity.setOnceLimit(DecimalUtils.convertYuanToFen(Double.valueOf(onceLimitNm)));
			}
			if (StringUtils.isNotEmpty(dayLimitNm)) {
				entity.setDayLimit(DecimalUtils.convertYuanToFen(Double.valueOf(dayLimitNm)));
			}
//			entity.setIsValid(BaseConstant.IS_YESNO_YES);
//			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			boolean success = this.custPayCfgDService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

    /**
     * 修改支付渠道管理子表状态
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modifySONSts.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object modifySONSts(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        String isValid = request.getParameter("isValid");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if(StringUtils.isEmpty(id)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "modifySONSts", "修改风控管理子表状态信息,主键参数为空！", CurrentThreadContext.getCurrentUserName());
            return result;
        }
        try {
            boolean success = this.custPayCfgDService.modifyRDIsvalid(Long.valueOf(id), Integer.parseInt(isValid));
            if(!success){
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "modifySONSts", "修改风控管理子表状态失败,ID:"+id, CurrentThreadContext.getCurrentUserName());
            } else {
                BaseLogUtils.info(logger, "modifySONSts", "修改风控管理子表状态成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
            }
        }catch(BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "modifySONSts", "修改风控管理子表状态失败,ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }catch(Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "modifySONSts", "修改风控管理子表状态失败,系统错误.ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }
}

